//
//  FKRefundDetailVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/3.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKRefundDetailVC.h"
#import "FKTableCell.h"
#import "FKRefundStatusCell.h"
#import "FKRefundExplainCell.h"
#import "FKOperationBtnStatus.h"
#import "FKInputOrderSnVC.h"
#import <GKPhotoBrowser.h>

@interface FKRefundDetailVC ()<FKRefundStatusCellDelegate, FKRefundExplainCellDelegate>

@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, copy) NSString *refundSn;

@end

@implementation FKRefundDetailVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"退款详情";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:@"refreshRefundDetailVC" object:nil];
    
    [self loadData];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *group = self.datas[indexPath.section];
    FKTableCell *cell = [FKTableCell cellWith:tableView className:group[@"cellName"]];
    cell.data = group[@"data"];
    cell.delegate = self;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *group = self.datas[indexPath.section];
    return [group[@"cellH"] floatValue];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark - FKRefundStatusCellDelegate
- (void)refundStatusCell:(FKRefundStatusCell *)cell didClickedStatusBtn:(NSString *)text
{
    if ([text isEqualToString:@"取消申请"]) {
        [self cancel];
    } else if ([text isEqualToString:@"填写订单号"]) {
        FKInputOrderSnVC *vc = [[FKInputOrderSnVC alloc] init];
        vc.refund_sn = self.refundSn;
        vc.order_refund_id = self.order_refund_id;
        fkPushNav(vc);
    }
}

#pragma mark - FKRefundExplainCellDelegate
- (void)refundExplainCell:(FKRefundExplainCell *)cell showPicture:(NSArray *)urls index:(NSInteger)index
{
    NSMutableArray *photos = [NSMutableArray new];
    for (NSString *url in urls) {
        GKPhoto *photo = [GKPhoto new];
        photo.url = [NSURL URLWithString:url];
        [photos addObject:photo];
    }
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:index];
    browser.showStyle = GKPhotoBrowserShowStyleNone;
    [browser showFromVC:self];
}


#pragma mark - 网络请求
- (void)loadData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"order_refund_id"] = self.order_refund_id;
    [FKHttpTool postWithSuffix:@"/OrderRefund/show" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            NSMutableArray *datas = [NSMutableArray array];
            
            NSDictionary *refund = result.data[@"refund"];
            self.refundSn = refund[@"refund_sn"];
            NSMutableDictionary *statusData = [NSMutableDictionary dictionaryWithDictionary:result.data[@"status_data"]];
            
            NSMutableDictionary *statusGroup = [NSMutableDictionary dictionary];
            [datas addObject:statusGroup];
            statusGroup[@"cellName"] = @"FKRefundStatusCell";
            statusGroup[@"data"] = statusData;
            
            NSArray *descArr = statusData[@"desc_arr"];
            CGFloat statusCellH = fk_adjustW(145);
            if (descArr.count > 0) {
                CGFloat maxW = fkScreenW - 12 - fk_adjustW(35);
                for (NSInteger index = 0; index < descArr.count; index++) {
                    CGFloat textH = [descArr[index] boundingRectWithSize:CGSizeMake(maxW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : fk_adjustFont(12)} context:nil].size.height;
                    if (index == 0) {
                        statusCellH += (fk_adjustW(15) + textH);
                    } else {
                        statusCellH += (textH + fk_adjustW(10));
                    }
                }
                statusCellH += (fk_adjustW(18));
            }
            
            // 设置按钮的状态
            NSDictionary *btn_arr = statusData[@"btn_arr"];
            if (btn_arr.count > 0) {
                NSMutableArray *btnStatus = [NSMutableArray array];
                if ([btn_arr[@"cancel_btn"] boolValue]) {
                    FKOperationBtnStatus *status = [FKOperationBtnStatus statusWith:@"取消申请" themeColor:fkColor333333];
                    [btnStatus addObject:status];
                }
                if ([btn_arr[@"ship_btn"] boolValue]) {
                    FKOperationBtnStatus *status = [FKOperationBtnStatus statusWith:@"填写订单号" themeColor:fkColor333333];
                    [btnStatus addObject:status];
                }
                statusData[@"btnStatus"] = btnStatus;
                
                if (btnStatus.count > 0) {
                    statusCellH += (fk_adjustW(30) + fk_adjustW(15));
                }
            }
            statusGroup[@"cellH"] = @(statusCellH);
            
            
            
            /**
             wait_check/买家发起了退款申请, 等待处理中
             cancelled/已取消售后申请 refuse/已拒绝
             wait_user_ship/待用户发货
             wait_merchant_confirm/用户已退货, 待退货确认
             complete/该售后申请已完成
             */
            NSString *state = refund[@"state"];
            if ([state isEqualToString:@"complete"]) {
                NSMutableDictionary *amountGroup = [NSMutableDictionary dictionary];
                [datas addObject:amountGroup];
                amountGroup[@"cellName"] = @"FKRefundAmountCell";
                amountGroup[@"data"] = refund;
                amountGroup[@"cellH"] = @(fk_adjustW(90));
            }
            
            NSMutableDictionary *infoGroup = [NSMutableDictionary dictionary];
            [datas addObject:infoGroup];
            infoGroup[@"cellName"] = @"FKRefundInfoCell";
            infoGroup[@"data"] = refund;
//            infoGroup[@"cellH"] = @(fk_adjustW(335));
            infoGroup[@"cellH"] = @(fk_adjustW(309));
            
            NSMutableDictionary *explainGroup = [NSMutableDictionary dictionary];
            [datas addObject:explainGroup];
            explainGroup[@"cellName"] = @"FKRefundExplainCell";

            CGFloat explainCellH = fk_adjustW(45);
            NSString *refund_content = refund[@"refund_content"];
            if (refund_content.length > 0) {
                CGFloat remarkH = [refund_content boundingRectWithSize:CGSizeMake(fkScreenW - 24, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : fk_adjustFont(14)} context:nil].size.height;
                explainCellH += (fk_adjustW(12) + remarkH + fk_adjustW(20));
            }
            NSMutableDictionary *explainData = [NSMutableDictionary dictionaryWithDictionary:refund];
            NSArray *refund_images = refund[@"refund_images"];
            if (refund_images.count > 0) {
                CGFloat margin = fk_adjustW(12);
                CGFloat imageWH = (fkScreenW - 24 - margin * 2) / 3.f;
                NSMutableArray *imageFrames = [NSMutableArray array];
                for (NSInteger index = 0; index < refund_images.count; index++) {
                    NSInteger row = index / 3;
                    NSInteger column = index % 3;
                    CGFloat imageX = 12 + column * (imageWH + margin);
                    CGFloat imageY = explainCellH + row * (imageWH + margin);
                    CGRect imageF = CGRectMake(imageX, imageY, imageWH, imageWH);
                    [imageFrames addObject:@(imageF)];

                    if (index == refund_images.count - 1) {
                        explainCellH = CGRectGetMaxY(imageF) + fk_adjustW(15);
                    }
                }
                explainData[@"imageFrames"] = imageFrames;
            }
            explainGroup[@"cellH"] = @(explainCellH);
            explainGroup[@"data"] = explainData;
            
            self.datas = datas;
            [self.tableView reloadData];
        }
    } failure:nil];
}

- (void)cancel
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"order_refund_id"] = self.order_refund_id;
    [FKHttpTool postWithSuffix:@"/OrderRefund/cancel" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showSuccessWithStatus:@"取消成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOrderListVC" object:nil];
            fkPopVC;
        }
    } failure:nil];
}


@end
